package genetic.q4;

import genetic.q4.functions.DoNothing;
import genetic.q4.functions.PutMinForOther;

import java.util.Vector;

public class TicTacToeBoard {
	
	public static final int WINNER_X = 1;
	public static final int WINNER_O = 2;
	public static final int WINNER_DRAW = 3;
	public static final int WINNER_NONE = 4;
	public static final int CLEAR = 0;
	public static final int X = 5;
	public static final int O = 6;
	int[] board;
	
	public TicTacToeBoard() {
		this.board = new int[9];
	}
	
	public void clear(){
		for (int i=0; i<9; i++){
			board[i] = 0;
		}
	}
	
	public int get(int index){
		return board[index];
	}
	
	public int getRandomEmptyPlace(){
		Vector<Integer> ans = new Vector<Integer>();
		for (int i=0; i<9; i++){
			if (board[i]==CLEAR){
				ans.add(i);
			}
		}
		if (ans.size()==0) return -1;
		
		int rand = (int)(Math.random()*ans.size());
		return ans.get(rand);
	}
	
	public int getMinEmptyPlace(){
		for (int i=0; i<9; i++){
			if (board[i]==CLEAR){
				return i;
			}
		}
		return -1;
	}	
	
	public boolean put(int side, int index){
		if (board[index] == CLEAR){
			board[index] = side;
			return true;
		} else {
			return false;
		}
	}
	
	public char getChar(int index){
		if (board[index] == X) return 'X';
		if (board[index] == O) return 'O';
		return ' ';
	}
	
	public int playGame(TicTacToeGenotype g1, TicTacToeGenotype g2){
		TicTacToeGenotype currentPlayer = g1;
		int currentColor = X;
		int numMoves = 0;
		while (numMoves<20 && getWinner()==WINNER_NONE){
			currentPlayer.play(this, currentColor);
//			System.out.println("#####");
//			System.out.println("#" + getChar(0) + getChar(1) + getChar(2) + "#");
//			System.out.println("#" + getChar(3) + getChar(4) + getChar(5) + "#");
//			System.out.println("#" + getChar(6) + getChar(7) + getChar(8) + "#");
//			System.out.println("#####");
			if (currentPlayer==g1){
				currentPlayer = g2;
				currentColor = O;
			} else {
				currentPlayer = g1;
				currentColor = X;		
			}
			numMoves++;
		}
		return getWinner();
	}
	
	public int getWinner(){
		int done = 1;
		for (int i=0; i<9; i++){
			done = done * board[i];
		}
		if (done == 0) return WINNER_NONE;
		
		if (board[0]==board[1] && board[1]==board[2]){
			if (board[0] == X) return WINNER_X;
			else return WINNER_O;
		}
		if (board[3]==board[4] && board[4]==board[5]){
			if (board[3] == X) return WINNER_X;
			else return WINNER_O;
		}
		if (board[6]==board[7] && board[7]==board[8]){
			if (board[6] == X) return WINNER_X;
			else return WINNER_O;
		}
		if (board[0]==board[3] && board[3]==board[6]){
			if (board[0] == X) return WINNER_X;
			else return WINNER_O;
		}
		if (board[1]==board[4] && board[4]==board[7]){
			if (board[1] == X) return WINNER_X;
			else return WINNER_O;
		}
		if (board[2]==board[5] && board[5]==board[8]){
			if (board[2] == X) return WINNER_X;
			else return WINNER_O;
		}
		if (board[0]==board[4] && board[4]==board[8]){
			if (board[0] == X) return WINNER_X;
			else return WINNER_O;
		}
		if (board[2]==board[4] && board[4]==board[6]){
			if (board[2] == X) return WINNER_X;
			else return WINNER_O;
		}
		return WINNER_DRAW;
	}
	
	public static void main(String[] args) {
		TicTacToeBoard b = new TicTacToeBoard();
		TicTacToeGenotype nothing = new TicTacToeGenotype(new DoNothing());
		TicTacToeGenotype pmfo = new TicTacToeGenotype(new PutMinForOther());
		System.out.println(b.playGame(nothing, pmfo));
		b.clear();
		System.out.println(b.playGame(pmfo, nothing));
	}
}
